package com.example.bailin.giftsay.sort.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.bailin.giftsay.R;
import com.example.bailin.giftsay.entity.SortColumnEntity;
import com.example.bailin.giftsay.sort.LmClickListener;
import com.example.bailin.giftsay.tool.MyApp;
import com.example.bailin.giftsay.tool.NetTool;

/**
 * Created by bailin on 16/8/17.
 * <p/>
 * 栏目的适配器
 * 已优化
 */
public class LanMuAdapter extends RecyclerView.Adapter<LanMuAdapter.LanMuViewHolder> {
    private SortColumnEntity entity;
    private LmClickListener clickListener;

    public void setClickListener(LmClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public void setEntity(SortColumnEntity entity) {
        this.entity = entity;
    }

    @Override
    public LanMuViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(MyApp.getContext()).inflate(R.layout.item_sorycolumn, parent, false);
        LanMuViewHolder viewHolder = new LanMuViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(LanMuViewHolder holder, final int position) {

        NetTool tool = new NetTool();
        tool.getImg(entity.getData().getColumns().get(position).getCover_image_url(), holder.ivColumnCoverImageUrl);
        holder.tvColumnTitle.setText(entity.getData().getColumns().get(position).getTitle());
        holder.tvColumnSubtitle.setText(entity.getData().getColumns().get(position).getSubtitle());
        holder.tvColumnAuthor.setText(entity.getData().getColumns().get(position).getAuthor());

        holder.rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                clickListener.onClick
                        (entity.getData().getColumns().get(position).getId()
                        ,entity.getData().getColumns().get(position).getTitle());
            }
        });
    }

    @Override
    public int getItemCount() {
        return entity.getData().getColumns() == null ? 0 : 12;
    }

    public class LanMuViewHolder extends RecyclerView.ViewHolder {
        public ImageView ivColumnCoverImageUrl; //大图片
        public TextView tvColumnTitle; // 标题
        public TextView tvColumnSubtitle; // 更新日期
        public TextView tvColumnAuthor; // 作者姓名
        public RelativeLayout rl;

        public LanMuViewHolder(View itemView) {
            super(itemView);
            ivColumnCoverImageUrl = (ImageView) itemView.findViewById(R.id.iv_column_cover_image_url);
            tvColumnTitle = (TextView) itemView.findViewById(R.id.tv_column_titlelm);
            tvColumnSubtitle = (TextView) itemView.findViewById(R.id.tv_column_subtitle);
            tvColumnAuthor = (TextView) itemView.findViewById(R.id.tv_column_author);
            rl = (RelativeLayout) itemView.findViewById(R.id.rl_item_lanmu);
        }
    }
}
